package com.itheima.shotplane.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;


import com.github.florent37.viewanimator.ViewAnimator;
import com.itheima.shotplane.PlaneWar;
import com.itheima.shotplane.R;
import com.itheima.shotplane.entity.Car;
import com.itheima.shotplane.entity.OtherCar;
import com.itheima.shotplane.listener.AddCarListener;
import com.itheima.shotplane.listener.AddHeroListener;

public class MainActivity extends Activity {
    private PlaneWar planeWar;
    ImageView mountain;
    TextView tishi;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        planeWar = (PlaneWar) View.inflate(this, R.layout.activity_main, null);
        setContentView(planeWar);
        mountain = (ImageView) findViewById(R.id.mountain);
        mountain .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                simpleAnimation();
            }
        });
        tishi = (TextView) findViewById(R.id.tishi);

        planeWar.start();

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int height = dm.heightPixels;       // 屏幕高度（像素）
        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
// 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        int screenHeight = (int) (height / density);// 屏幕高度(dp)
        Log.e("width=======>",width+"");
        Log.e("height=======>",height+"");


        addcar_1();
        addcar_2();
        mHandler.postDelayed(r, 1000);//延时100毫秒
        simpleAnimation();
    }

    int downmy_x = 1500;
    int downmy_y = 600;
    int downother_x = 1500;
    int downother_y = 600;
    int chushi=10;
    int i=0;
    boolean jinxing=false;
    Handler mHandler = new Handler();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            //do something
            //每隔1s循环执行run方法
              mHandler.postDelayed(this, 100);
              downmy_y = downmy_y - 3;
              downother_y =   downother_y -chushi;
              //我的车小于120，回到1400处重新跑
            if (downmy_y < 120) {
                downmy_y = 1500;
            }
            //另一辆车小于120，回到1400处重新跑
            if (downother_y < 80) {
                downother_y = 1500;
            }

            //即将发生碰撞
            if (downother_x==downmy_x&&downother_y-downmy_y<200) {
                downother_x = 1100;
                Log.e("即将发生碰撞", "即将发生碰撞"+downother_x);
                tishi.setText("无预警");
                tishi.setTextColor(getResources().getColor(R.color.white));
                jinxing=true;
            }else{
                jinxing=false;
            }
//            if (jinxing) {
//                i++;
//                if (i==6) {
//                    //前车靠近
//                    downother_y = 1500;
//                    chushi=2;
//                }else{
//                    i=0;
//                    downother_y = 1100;
//                    chushi=10;
//                }
//
//
//            }
            //即将预警
            if (downother_x==downmy_x&&downother_y-downmy_y<350) {
                Log.e("xxxx", "快碰撞了，跑啊！");
                tishi.setText("后方盲区超车预警");
                tishi.setTextColor(getResources().getColor(R.color.red));
            }
            //另一辆车Y小于400，变道
            if (downother_y < 400) {
                downother_x = 1500;
                downother_y=1500;
                Log.e("变道", "变道");
            }
//            else {
//                downother_x = 1000;
//            }
//            Log.e("测试", "downmy_x=" + downmy_x+",downother_x=" + downother_x);
            Log.e("测试", "downother_y=" + downother_y+",downother_x=" + downother_x );
            planeWar.moveHeroTo(downmy_x, downmy_y);
            planeWar.moveHeroToOther(downother_x, downother_y);

        }
    };

    public void addcar_1() {

        Car mCar = new Car(MainActivity.this);
        mCar.setX(downmy_x);
        mCar.setSety(downmy_y);
        mCar.setVisibility(View.INVISIBLE);
        mCar.getViewTreeObserver().addOnGlobalLayoutListener(
                new AddHeroListener(mCar));
        planeWar.setmCar(mCar);
        planeWar.addView(mCar);
    }

    public void addcar_2() {

        OtherCar mCar2 = new OtherCar(MainActivity.this);
        mCar2.setX(downother_x);
        mCar2.setSety(downother_y);
        mCar2.setVisibility(View.INVISIBLE);
        mCar2.getViewTreeObserver().addOnGlobalLayoutListener(
                new AddCarListener(mCar2));
        planeWar.setOtherCar(mCar2);
        planeWar.addView(mCar2);
    }

    protected void simpleAnimation() {
        ViewAnimator.animate(mountain)
                .translationY(-1000, 0)
                .alpha(0, 1)
//                .andAnimate(text)
                .translationX(-200, 0)
                .interpolator(new DecelerateInterpolator())
                .duration(2000)
                .thenAnimate(mountain)
                .duration(1000)

                .start();
    }
}
